<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Model;
use Think\Model;

/**
 * 文档基础模型
 */
class OrderModel extends Model{
	protected $_validate = array(
		array('create_time', NOW_TIME, self::MODEL_INSERT),
	);
	public function update($order){
		$this ->create($order);
		$retrun=$this->add($order);
		if($retrun){
			return $retrun;
		}else{
			return false;
		}
	}
	public function seav($out){
		$this->where(array('order'=>$out))->setField('status',1);
	}
	/**
	 * 不同意退订
	 */
	public function notCancel($order){
		$data['cancel_time'] = "";
		$data['cancel']      = 0;
		return D('Home/Mycourse') -> where("`order` = $order") ->save($data);
	}
	/**
	 * 撤销的方法
	 */
	public function chexiao($order){
		$data = $this ->where(array('order' =>$order)) ->find();
		$where['order_id'] = $data['id'];
		$where['class_date'] = array('gt',time());
		$map['class_date'] = 0;
		$result = D('CourseRecord') ->where($where) ->save($map);
		return $result;
	}
	/*
	 *给老师发消息
	 */
	public function teacheMsg($order){
		$map['order'] = $order;
		$data = $this ->where($map) ->find();
		$result = D('UserMember') ->field('nickname') ->where(array('id' => $data['uid'])) ->find();
		$arr[] = array(
			'sid'     => 0,
			'rid'     => $data['tid'],
			'content' => "您的学生".$result['nickname']."已取消退订",
			'type'    => 4,
			'time'    => time()
			);
		$arr[] = array(
			'sid'     => 0,
			'rid'     => $data['uid'],
			'content' => "您的退订已取消。",
			'type'    => 4,
			'time'    => time()
			);
		D('Message') ->addAll($arr);
	}
	//判断是否有没确认课程的
	public function isAt_class($order){
		$map['o.order'] = $order;
		$map['r.at_class'] = 0;
		$time = time();
		$map['r.class_date'] = array(between,array('1',"$time"));
		$result = $this ->alias('o')
			  ->field(['r.class_date'])
			  ->join('tutor_course_record as r on o.id = r.order_id')
			  ->where($map)
			  ->order('r.id desc')
			  ->find();
			 // echo M()->_Sql();
		return $result;
	}
}
